package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liangyaoyun
 */
public class leetCode46 {

    public List<Integer>path=new ArrayList<>();
    public List<List<Integer>>result=new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        boolean[] flag=new boolean[nums.length];
        dfs(nums,0,flag);
        return  result;
    }

    public void dfs(int[] nums,int index,boolean[] flag){
        if (index>=nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            //如果这个数未被取过，则将它加入路径之中
            if (!flag[i]){
                path.add(nums[i]);
                flag[i]=true;
                dfs(nums,index+1,flag);

                flag[i]=false;
                path.remove(path.size()-1);
            }
//
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        leetCode46 leetcode46=new leetCode46();
        leetcode46.permute(nums);
    }
}
